package com.yhy.server.controller;

import com.yhy.domain.mongo.Publish;
import com.yhy.server.service.CommentService;
import com.yhy.server.service.MovementsMQService;
import com.yhy.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private MovementsMQService movementsMQService;

    @PostMapping
    public ResponseEntity publishMovements(Publish publish,
                                    MultipartFile[] imageContent) throws IOException {
        return movementsService.saveMovements(publish,imageContent);
    }

    @GetMapping
    public ResponseEntity findMovements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        return movementsService.findMovements(page,pagesize);
    }

    @GetMapping("/recommend")
    public ResponseEntity recommendMovements(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pagesize){
        return movementsService.recommendMovements(page,pagesize);
    }

    /**
     * 查看自己发布的动态
     *      /movements/all
     */
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize,
                              Long userId){
        return movementsService.findOwnMovements(page,pagesize,userId);
    }

    /**
     * 根据publishID查看movements详情
     *
     *   /movements/:id
     */
    @GetMapping("/{id}")
    public ResponseEntity findMovementDetailById(@PathVariable("id") String publishId){
        movementsMQService.sendMsg(publishId,1); //type 1 浏览
        return movementsService.findMovementDetailById(publishId);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String publishId){
        movementsMQService.sendMsg(publishId,2);//type 2 点赞
        return commentService.like(publishId);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String publishId){
        movementsMQService.sendMsg(publishId,5);//type 5 取消点赞
        return commentService.dislike(publishId);
    }

    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String publishId){
        movementsMQService.sendMsg(publishId,3);//type 3 喜欢
        return commentService.love(publishId);
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String publishId){
        movementsMQService.sendMsg(publishId,6);//type 6 取消喜欢
        return commentService.unlove(publishId);
    }

    /**
     * 谁看过我
     *
     * 接口路径：GET/movements/visitors
     *
     * 参数: 无
     *
     * 返回值: TodayBest数组
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        return movementsService.visitors();
    }
}